# class Solution:
#     def maxProfit(self, prices: List[int], fee: int) -> int:
#         profit=0
#         buyed=False
#         buyprice=0
#         sellprice=0
#         sellindex=len(prices)
#         for i in range(len(prices)):
#             if not buyed:
#                 buyprice=prices[i]
#                 buyed=True
#                 #print("a",prices[i])
#                 continue
#             if buyed and prices[i]<buyprice and i<sellindex:
#                 buyprice=prices[i]
#                 print("b",prices[i])
#                 continue
#             if buyed and prices[i]-buyprice>fee and prices[i]>sellprice:
#                 sellprice=prices[i]
#                 sellindex=i
#                 #print("c",prices[i])
#                 continue
#             if buyed and prices[i]>sellprice and sellindex!=len(prices):
#                 sellprice=prices[i]
#                 sellindex=i
#                 #print("d",prices[i])
#                 continue
#             if buyed and sellprice-prices[i]>fee:
#                 sellindex=len(prices)
#                 profit+=sellprice-buyprice-fee
#                 buyprice=prices[i]
#                 sellprice=0
#                 #print("e",prices[i])
#                 continue
#         if buyed and sellindex!=len(prices):
#             profit+=sellprice-buyprice-fee
#         #print(sellprice,buyprice)
#         return profit

'''
执行用时：
708 ms
, 在所有 Python3 提交中击败了
98.83%
的用户
内存消耗：
22.1 MB
, 在所有 Python3 提交中击败了
38.00%
的用户
'''

class Solution:
    def maxProfit(self, prices: List[int], fee: int) -> int:
        profit=0
        buyed=False
        buyprice=0
        sellprice=0
        for i in range(len(prices)):
            if not buyed:
                buyprice=prices[i]
                buyed=True
                #print("a",prices[i])
                continue
            if buyed and prices[i]-buyprice>fee and prices[i]>sellprice:
                sellprice=prices[i]
                #print("c",prices[i])
                continue
            if buyed and prices[i]>sellprice and sellprice!=0:
                sellprice=prices[i]
                #print("d",prices[i])
                continue
            if buyed and sellprice-prices[i]>fee:
                sellindex=len(prices)
                profit+=sellprice-buyprice-fee
                buyprice=prices[i]
                sellprice=0
                #print("e",prices[i])
                continue
            if buyed and prices[i]<buyprice:
                buyprice=prices[i]
                #print("b",prices[i])
                continue
        if buyed and sellprice:
            profit+=sellprice-buyprice-fee
        #print(sellprice,buyprice)
        return profit

'''
执行用时：
684 ms
, 在所有 Python3 提交中击败了
99.48%
的用户
内存消耗：
21.4 MB
, 在所有 Python3 提交中击败了
38.40%
的用户
'''